package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.mrobuy.jdvc.dao.VCItemPicDao;
import com.mrobuy.jdvc.entity.VCItemPic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("vcItemPicDao")
public class VCItemPicDaoImpl extends CommonDaoImpl implements VCItemPicDao {

    private static Logger LOG = LoggerFactory.getLogger(VCItemPicDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCItemPic> load(String wareId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM_PIC where WAREID = ? ");
        try {
            List<VCItemPic> list = jdbcTemplate.query(sql.toString(), new Object[]{wareId}, new BeanPropertyRowMapper(VCItemPic.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public void update(VCItemPic vcItemPic, boolean updateNull) {
        try {
            SQLExecutor.tempLateUpate(vcItemPic, jdbcTemplate, updateNull);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<VCItemPic> load(String skuid, String type) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM_PIC where WAREID = ? and TYPE = ? ");
        try {
            List<VCItemPic> list = jdbcTemplate.query(sql.toString(), new Object[]{skuid, type}, new BeanPropertyRowMapper(VCItemPic.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public VCItemPic loadById(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM_PIC where ID = ? ");
        try {
            VCItemPic record = (VCItemPic)jdbcTemplate.queryForObject(sql.toString(), new Object[]{id}, new BeanPropertyRowMapper(VCItemPic.class));
            return record;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
